import React from 'react';

type ParsedChild<T> = T & { key?: string; node: React.ReactElement };

export function parseChildList<T extends Record<string, unknown>>(
  children: React.ReactNode,
): ParsedChild<T>[] {
  return React.Children.toArray(children)
    .map((node) => {
      if (React.isValidElement<T>(node)) {
        const key = node.key !== undefined ? String(node.key) : undefined;
        return {
          key,
          ...(node.props as T),
          node,
        } as ParsedChild<T>;
      }
      return null;
    })
    .filter(Boolean) as ParsedChild<T>[];
}
